home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / sys / XIPRunClass.elf < prev    next >
Text File  |  2009-04-23  |  19KB  |  602 lines

  1. /* $Id: XIPRunClass.elf,v 1.7 2009/03/03 21:44:41 kingsley Exp $ */
  2.  
  3. /* This file contains script to parse xml that contains a script (either by
  4. ** name or the actual script) and parameters. The script begins near the end
  5. ** of the file. Currently this supports two schemas that define the XML
  6. ** document to parse.
  7. */
  8. #load "sys/xipxml.elf";
  9.  
  10. /*****************************************************************************/
  11. /*
  12. ** @New This class implements the parser for the RunXIP schema. The root
  13. ** contains an input element which contains a script element and a
  14. ** params element. The script element may name a script file or it
  15. ** contain the actual script.
  16. */
  17. /* @setVocal Output messages as XML is parsed. */ 
  18. /* @parseXPath Given an XPath object, parse the XML and initialize this. */
  19. /* @getReturnXML Get an XML document with the results of script execution. */
  20. /* @getScriptName What is the name of the script to execute? */ 
  21. /* @getParamList Get a LIST that contains the import values for the script. */
  22. /* @getReturnList Get a LIST that contains the returns values for the script. */
  23. /*****************************************************************************/
  24. private
  25. CLASS XIPRunClass
  26. {
  27.     METHOD New() {}
  28.     METHOD setVocal (BOOLEAN setTo) {
  29.         this.isVocal = setTo;
  30.     }
  31.  
  32.     METHOD parseXPath ( XPath xpath ) RETURNS ( BOOLEAN success )
  33.     {
  34.         if (this.isVocal == TRUE) print "Initialize XIPRunClass via XPath";
  35.         success  = this.parseScript( xpath: xpath );
  36.         if (success == FALSE)
  37.         {
  38.             if (this.isVocal == TRUE) print "Failed in parseScript";
  39.             this.isValid = FALSE;
  40.             return;
  41.         }
  42.  
  43.         success = this.parseParams( xpath: xpath );
  44.         if (success == FALSE)
  45.         {
  46.             if (this.isVocal == TRUE) print "Failed in parseParams";
  47.             this.isValid = FALSE;
  48.             return;
  49.         }
  50.         
  51.         success = this.parseReturns( xpath: xpath );
  52.         if (success == FALSE)
  53.         {
  54.             if (this.isVocal == TRUE) print "Failed in parseReturns";
  55.             this.isValid = FALSE;
  56.             return;
  57.         }
  58.         this.isValid = TRUE;
  59.         if (this.isVocal == TRUE) print "Initialization successful";
  60.     }
  61.  
  62.  
  63.     METHOD getReturnXML ( LIST exportList )
  64.                 RETURNS ( STRING rXml, BOOLEAN erCatch )
  65.     {
  66.         if (this.isVocal == TRUE) print "XIPRunClass:getReturnXML";
  67.         XmlDocumentBuilder rtdb;
  68.         XmlDocument rtdoc = rtdb.newDocument();
  69.         XmlElement rtroot = rtdoc.createElement( name: "RunXIP" );
  70.         rtdoc.setDocumentElement( node: rtroot );
  71.         XmlElement rtout = rtdoc.createElement( name: "Output" );
  72.         rtroot.appendChild ( node: rtout );
  73.  
  74.         INTEGER icnt = this.retTypes.length();
  75.         INTEGER i;
  76.         STRING rtype;
  77.         STRING rname;
  78.         OBJECT eobj;
  79.  
  80.         erCatch = FALSE;
  81.         // get error and warning messages - always add these
  82.         i = exportList.search(name: "runxipError");
  83.         if (i != -1) {
  84.             erCatch = TRUE;
  85.             eobj = exportList.ref( entry: i );
  86.             this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
  87.                                         name: "runxipError", datatype: "String" );
  88.         }
  89.         i = exportList.search(name: "runxipWarning");
  90.         if (i != -1) {
  91.             erCatch = TRUE;
  92.             eobj = exportList.ref( entry: i );
  93.             this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
  94.                                         name: "runxipWarning", datatype: "String" );
  95.         }
  96.         for ( i=0; i<icnt; i++ )
  97.         {
  98.             rname = this.retNames.ref( entry: i );
  99.             rtype = this.retTypes.ref( entry: i );
  100.             eobj = exportList.ref( name: rname );
  101.             if ((eobj) || (rtype == "Integer"))
  102.                 this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
  103.                                         name: rname, datatype: rtype );
  104.         }
  105.         if (this.isVocal == TRUE) {
  106.             print "Returning Document is:";
  107.             print rtdoc.dumpString( format: 1 );
  108.         }
  109.         rXml = rtdoc.dumpString( format: 0 );
  110.     }
  111.  
  112.  
  113.     METHOD getScriptName () RETURNS ( STRING name )
  114.     {
  115.         name = this.name;
  116.     }
  117.  
  118.  
  119.     METHOD getParamList () RETURNS ( LIST params )
  120.     {
  121.         params = this.params;
  122.     }
  123.  
  124.  
  125.     METHOD getReturnList () RETURNS ( LIST retvals )
  126.     {
  127.         retvals = this.retVals;
  128.     }
  129.     
  130.     METHOD getIsValid () RETURNS ( BOOLEAN iv )
  131.     {
  132.         iv = this.isValid;
  133.     }
  134.  
  135.  
  136.     METHOD getScriptValue () RETURNS ( STRING sv )
  137.     {
  138.         sv = this.scriptValue;
  139.     }
  140.  
  141.  
  142.     METHOD getIsBlock () RETURNS ( BOOLEAN ib )
  143.     {
  144.         ib = this.isBlock;
  145.     }
  146.  
  147.  
  148.     /*****************************************************************************
  149.     ********************    Lots of Private Methods ******************************
  150.     *****************************************************************************/
  151.  
  152.     private METHOD addReturnValXML( XmlDocument xmlDoc, XmlElement outEle, OBJECT obj,
  153.                                  STRING name, STRING datatype )
  154.     {
  155.         XmlAttr rtAttr;
  156.         XmlElement rtelm = xmlDoc.createElement( name: "ReturnVal" );
  157.         XmlElement valName = xmlDoc.createElement( name: "Name" );
  158.         //XmlElement valVal = xmlDoc.createElement( name: "Value" );
  159.         STRING objAsStr = obj;
  160.  
  161.         /* Add the ArgType attribute to the ReturnVal element */
  162.         rtAttr = xmlDoc.createAttribute(name: "ArgType");
  163.         rtAttr.setValue( value: datatype );
  164.         rtelm.setAttributeNode( node: rtAttr );
  165.  
  166.         /* Add a child element that contains the Val's name */
  167.         valName.setNodeValue( value: name );
  168.         rtelm.appendChild ( node: valName );
  169.  
  170.         /* Add a child element that contains the actual value */
  171.         LIST list;
  172.         if (obj.Type() == "LIST")
  173.         {
  174.             list = obj;
  175.             rtelm.setAttribute(name: "IsList", value: "true");
  176.         }
  177.         else
  178.         {
  179.             list.insert(obj: obj);
  180.         }
  181.         this.addReturnValXmlValues(xmlDoc: xmlDoc, rtelm: rtelm, list: list);
  182.         
  183.         /* put the new element (ReturnVal) into the Output element */
  184.         outEle.appendChild ( node: rtelm );
  185.     }
  186.     
  187.     private METHOD addReturnValXmlValues(XmlDocument xmlDoc, XmlElement rtelm, LIST list)
  188.     {
  189.         INTEGER i;
  190.         XmlElement valVal;
  191.         STRING name;
  192.         STRING str;
  193.         XmlAttr valAttr;
  194.  
  195.         for(i = 0; i < list.length(); i++)
  196.         {
  197.             name = list.name(entry: i);
  198.         `   str = list[i]; 
  199.             valVal = xmlDoc.createElement( name: "Value" );
  200.             valVal.setNodeValue( value: str );
  201.             valVal.setAttribute(name: "Name", value: name);
  202.             rtelm.appendChild ( node: valVal );
  203.         }
  204.     }
  205.  
  206.     private METHOD parseScript ( XPath xpath ) RETURNS ( BOOLEAN success )
  207.     {
  208.         // Example
  209.         // <Name>My Script</Name> 
  210.         // <Type>block</Type> 
  211.         // <Value>yada, yada</Value>
  212.  
  213.         /* Test for the existance of script element */
  214.         XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/Script" );
  215.         if (!nset || (nset.getLength() < 1) )
  216.         {
  217.             SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script element in XPath" );
  218.             success = FALSE;
  219.             this.isValid = FALSE;
  220.             return;
  221.         }
  222.  
  223.         /* Test for the existance of script name...not required, use first one - index 0 */
  224.         nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Name" );
  225.         if (nset && (nset.getLength() > 0) ) {
  226.             this.name = this.innerText( xelem: nset.item( index: 0) );
  227.         }
  228.  
  229.         nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Type" );
  230.         if (!nset || (nset.getLength() < 1) )
  231.         {
  232.             SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script/type element in XPath" );
  233.             success = FALSE;
  234.             this.isValid = FALSE;
  235.             return;
  236.         }
  237.         this.isBlock = this.innerText( xelem: nset.item( index: 0) ).strcasecmp( str: "block" );
  238.  
  239.         nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Value" );
  240.         if (!nset || (nset.getLength() < 1) )
  241.         {
  242.             SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script/Value element in XPath" );
  243.             success = FALSE;
  244.             this.isValid = FALSE;
  245.             return;
  246.         }
  247.         this.scriptValue = this.innerText( xelem: nset.item( index: 0) );
  248.         success = TRUE;
  249.     }
  250.  
  251.  
  252.     private METHOD parseParams ( XPath xpath ) RETURNS ( BOOLEAN success )
  253.     {
  254.         // Example
  255.         // <Param ArgType="Integer" IsList="true">
  256.         //   <Name>aParam</Name> 
  257.         //   <Value>27</Value>
  258.         // </Param>
  259.         
  260.         // Zero param elements is okay
  261.         XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/Param" );
  262.         if (!nset )
  263.         {
  264.             success = TRUE;
  265.             return;
  266.         }
  267.         INTEGER ncnt = nset.getLength();
  268.         INTEGER i;
  269.         XmlElement xe;
  270.         BOOLEAN suc;
  271.         for (i=0; i<ncnt; i++)
  272.         {
  273.             xe = nset.item ( index: i );
  274.             success = this.appendParam ( xelem: xe );
  275.             if ( success == FALSE )
  276.             {
  277.                 return;
  278.             }  
  279.         }
  280.         success = TRUE;
  281.     }
  282.  
  283.  
  284.     private METHOD parseReturns ( XPath xpath ) RETURNS ( BOOLEAN success )
  285.     {
  286.         // Example
  287.         // <Return ArgType="Integer" ArgLength="1">
  288.         //   <Name>aParam</Name> 
  289.         // </Return>
  290.         
  291.         // Zero param elements is okay
  292.         XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/ExpectedReturnVal" );
  293.         if (!nset )
  294.         {
  295.             success = TRUE;
  296.             return;
  297.         }
  298.         INTEGER ncnt = nset.getLength();
  299.         INTEGER i;
  300.         XmlElement xe;
  301.         BOOLEAN suc;
  302.         for (i=0; i<ncnt; i++)
  303.         {
  304.             xe = nset.item ( index: i );
  305.             success = this.appendReturn ( xelem: xe );
  306.             if ( success == FALSE )
  307.             {
  308.                 return;
  309.             }  
  310.         }
  311.         success = TRUE;
  312.     }
  313.  
  314.  
  315.     private METHOD appendParam ( XmlElement xelem ) RETURNS ( BOOLEAN success )
  316.     {
  317.         // Example - this is a list of integers
  318.         // <Param ArgType="Integer">
  319.         //   <Name>aParam</Name> 
  320.         //   <Value>27</Value>
  321.         //   <Value>43</Value>
  322.         // </Param>
  323.  
  324.         STRING datatype = xelem.getAttribute( name: "ArgType" );
  325.         STRING listTest = xelem.getAttribute( name: "IsList" );
  326.         LIST vals = this.fetchChildrenByName ( parent: xelem, cName: "Value" );
  327.         XmlElement nm = this.fetchChild ( parent: xelem, cName: "Name" );
  328.         INTEGER dataCnt = vals.length();
  329.         XmlElement val;
  330.  
  331.         if ((dataCnt > 1) || (listTest.strcasecmp(str: "true") == TRUE))  /* this is a collection */
  332.         {
  333.             INTEGER i;
  334.             LIST collect;
  335.             STRING aName = this.innerText( xelem: nm );
  336.             for (i=o; i<dataCnt; i=i+1)
  337.             {
  338.                 val = vals.ref( entry: i );
  339.                 this.insertEntryByType (
  340.                     obj: this.innerText( xelem: val ),
  341.                     atloc: 10000,
  342.                     oftype: datatype,
  343.                     intolist: collect);
  344.             }
  345.             this.insertNameByType (
  346.                     obj: collect,
  347.                     ofname: this.innerText( xelem: nm ),
  348.                     oftype: "list",
  349.                     intolist: this.params);
  350.         }
  351.         else
  352.         {
  353.             val = vals.ref( entry: 0 );
  354.             this.insertNameByType (
  355.                 obj: this.innerText( xelem: val ),
  356.                 ofname: this.innerText( xelem: nm ),
  357.                 oftype: datatype,
  358.                 intolist: this.params);
  359.         }
  360.         success = TRUE;
  361.     }
  362.  
  363.  
  364.     private METHOD insertNameByType (OBJECT obj, STRING ofname, STRING oftype, LIST intolist)
  365.     {
  366.         INTEGER iDt;
  367.         DOUBLE dDt;
  368.         BOOLEAN bDt;
  369.         STRING sDt;
  370.         LIST lDt;
  371.         
  372.         if ( oftype.strcasecmp( str: "integer" ) == TRUE )
  373.         {
  374.             iDt = obj;   // cast to integer
  375.             intolist.insert( name: ofname, obj: iDt );
  376.         }
  377.         else if ( oftype.strcasecmp( str: "string" ) == TRUE )
  378.         {
  379.             sDt = obj;   // cast to string
  380.             intolist.insert( name: ofname, obj: sDt );
  381.         }
  382.         else if ( oftype.strcasecmp( str: "double" ) == TRUE )
  383.         {
  384.             dDt = obj;   // cast to double
  385.             intolist.insert( name: ofname, obj: dDt );
  386.         }
  387.         else if ( oftype.strcasecmp( str: "boolean" ) == TRUE )
  388.         {
  389.             if (obj.strcasecmp(str: "false"))
  390.                 bDt = FALSE;
  391.             else if ((obj.strcasecmp(str: "true")) || (obj == 1))
  392.                 bDt = TRUE;
  393.             else
  394.                 bDt = FALSE;
  395.             intolist.insert( name: ofname, obj: bDt );
  396.         }
  397.         else if ( oftype.strcasecmp( str: "list" ) == TRUE )
  398.         {
  399.             lDt = obj;   // cast to LIST
  400.             intolist.insert( name: ofname, obj: lDt );
  401.         }
  402.     }
  403.  
  404.  
  405.     private METHOD insertEntryByType (OBJECT obj, INTEGER atloc, STRING oftype, LIST intolist)
  406.     {
  407.         INTEGER iDt;
  408.         DOUBLE dDt;
  409.         BOOLEAN bDt;
  410.         STRING sDt;
  411.         LIST lDt;
  412.         
  413.         if ( oftype.strcasecmp( str: "integer" ) == TRUE )
  414.         {
  415.             iDt = obj;   // cast to integer
  416.             intolist.insert( entry: atloc, obj: iDt );
  417.         }
  418.         else if ( oftype.strcasecmp( str: "string" ) == TRUE )
  419.         {
  420.             sDt = obj;   // cast to string
  421.             intolist.insert( entry: atloc, obj: sDt );
  422.         }
  423.         else if ( oftype.strcasecmp( str: "double" ) == TRUE )
  424.         {
  425.             dDt = obj;   // cast to double
  426.             intolist.insert( entry: atloc, obj: dDt );
  427.         }
  428.         else if ( oftype.strcasecmp( str: "boolean" ) == TRUE )
  429.         {
  430.             if (obj.strcasecmp(str: "false"))
  431.                 bDt = FALSE;
  432.             else if ((obj.strcasecmp(str: "true")) || (obj == 1))
  433.                 bDt = TRUE;
  434.             else
  435.                 bDt = FALSE;
  436.             intolist.insert( entry: atloc, obj: bDt );
  437.         }
  438.         else if ( oftype.strcasecmp( str: "list" ) == TRUE )
  439.         {
  440.             lDt = obj;   // cast to LIST
  441.             intolist.insert( entry: atloc, obj: lDt );
  442.         }
  443.     }
  444.  
  445.  
  446.     private METHOD appendReturn ( XmlElement xelem ) RETURNS ( BOOLEAN success )
  447.     {
  448.         // Example
  449.         // <Return ArgType="Integer" ArgLength="1">
  450.         //   <Name>aParam</Name> 
  451.         // </Return>
  452.         INTEGER iDt;
  453.         DOUBLE dDt;
  454.         BOOLEAN bDt;
  455.  
  456.         STRING datatype = xelem.getAttribute( name: "ArgType" );
  457.         STRING dataCnt = 1;
  458.         if ( xelem.hasAttribute( name: "AgrLength" ) == TRUE)
  459.             dataCnt = xelem.getAttribute( name: "ArgLength" );
  460.  
  461.         XmlElement nm = this.fetchChild ( parent: xelem, cName: "Name" );
  462.         STRING inText = this.innerText( xelem: nm );
  463.         this.retVals.insert( name: inText, obj: 1 );
  464.         this.retTypes.insert( obj: datatype );
  465.         this.retNames.insert( obj: inText );
  466.         success = TRUE;
  467.     }
  468.  
  469.  
  470.     private METHOD fetchChildrenByName (XmlNode parent, STRING cName) RETURNS (LIST kids)
  471.     {
  472.         XmlNodeList nl = parent.getChildNodes();
  473.         INTEGER icnt = nl.getLength();
  474.         INTEGER i;
  475.         XmlNode member;
  476.         XmlElement xe;
  477.  
  478.         for (i=0; i<icnt; i++)
  479.         {
  480.             member = nl.item( index: i );
  481.             if (member.getNodeType() == XmlNode.XML_ELEMENT_NODE)
  482.             {
  483.                 xe = member;
  484.                 if ( xe.getTagName().strcasecmp( str: cName ) == TRUE )
  485.                 {
  486.                     kids.insert(entry: 10000, obj: xe);
  487.                 }
  488.             }
  489.         }
  490.     }
  491.  
  492.  
  493.     private METHOD fetchChild (XmlNode parent, STRING cName) RETURNS (XmlNode elem)
  494.     {
  495.         XmlNodeList nl = parent.getChildNodes();
  496.         elem = this.fetchElement ( nList: nl, eName: cName );
  497.     }
  498.  
  499.  
  500.  
  501.     /* Gets the first element matching the name */
  502.     private METHOD fetchElement (XmlNodeList nList, STRING eName) RETURNS (XmlNode elem)
  503.     {
  504.         INTEGER icnt = nList.getLength();
  505.         INTEGER i;
  506.         XmlNode member;
  507.         XmlElement xe;
  508.         
  509.         for (i=0; i<icnt; i++)
  510.         {
  511.             member = nList.item( index: i );
  512.             if (member.getNodeType() == XmlNode.XML_ELEMENT_NODE)
  513.             {
  514.                 xe = member;
  515.                 if ( xe.getTagName().strcasecmp( str: eName ) == TRUE )
  516.                 {
  517.                     elem = member;
  518.                     return;
  519.                 }
  520.             }
  521.         }
  522.     }
  523.  
  524.  
  525.     private METHOD innerText( XmlNode xelem ) RETURNS ( STRING str )
  526.     {
  527.         XmlText xt = xelem.getFirstChild();
  528.         if (!xt)
  529.         {
  530.             SetStatus( op: "stop", msg: "XIPRunClass: Not able to get Inner Text" );
  531.             this.isValid = FALSE;
  532.             return;
  533.         }
  534.         str = xt.getData();
  535.     }
  536.  
  537.  
  538.     private METHOD printChildNodes( xmlNode target )
  539.     {
  540.         XmlNodeList nList = target.getChildNodes();
  541.         if (!nList)
  542.         {
  543.             print "No child nodes found for "+target.getNodeName();
  544.         }
  545.         this.printNodeList( nList: nList );
  546.     }
  547.  
  548.  
  549.     private METHOD printNodeList ( XmlNodeList nList )
  550.     {
  551.         INTEGER icnt = nList.getLength();
  552.         INTEGER i;
  553.         XmlNode member;
  554.         
  555.         print "Node list member count: "+icnt;
  556.         for (i=0; i<icnt; i++)
  557.         {
  558.             member = nList.item( index: i );
  559.             this.printNode( target: member );
  560.         }
  561.     }
  562.  
  563.  
  564.     private METHOD printNode ( xmlNode target )
  565.     {
  566.         INTEGER typeid = target.getNodeType();
  567.         XmlText xt;
  568.         XmlElement xe;
  569.  
  570.         if (typeid == XmlNode.XML_TEXT_NODE)
  571.         {
  572.             xt = target;
  573.             print "text data is ";
  574.             print xt.getData()+" with length "+xt.getLength();
  575.         }
  576.         else if (typeid == XmlNode.XML_ELEMENT_NODE)
  577.         {
  578.             xe = target;
  579.             print "Element name is ";
  580.             print xe.getTagName();
  581.         }
  582.         else
  583.         {
  584.             print "XmlNode "+target.getNodeName()+" of type "+typeid+" not known in printNode";
  585.         }
  586.     }
  587.  
  588.   
  589.     /*****************************************************************************
  590.     ********************    Internal Data Fields    ******************************
  591.     *****************************************************************************/
  592.     LIST params;
  593.     LIST retVals;
  594.     LIST retNames;
  595.     LIST retTypes;
  596.     STRING name = "Not Done";
  597.     STRING scriptValue;
  598.     BOOLEAN isValid = FALSE;
  599.     BOOLEAN isBlock = FALSE;
  600.     BOOLEAN isVocal = FALSE;
  601. }
  602.